﻿using MediatR;
using SmartCity.ProcessEngine.API.Read.Application.Commands.Forms;
using SmartCity.ProcessEngine.API.Read.DTO;
using SmartCity.ProcessEngine.Domain;
using SmartCity.ProcessEngine.Errorcode;
using SmartCity.ProcessEngine.Infrastructure.Interfaces;

namespace SmartCity.ProcessEngine.API.Read.Application.CommandHandlers.Forms
{
    /// <summary>
    /// 表单分类处理
    /// </summary>
    public class FormTypeCascaderQueryCommandHandler:IRequestHandler<FormTypeCascaderQueryCommand,ApiResult<List<FormTypeDto>>>
    {
        private readonly IBaseRepository<WF_FromType> typeRep;

        public FormTypeCascaderQueryCommandHandler(IBaseRepository<WF_FromType> typeRep)
        {
            this.typeRep = typeRep;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回表单分类集合任务</returns>
        public Task<ApiResult<List<FormTypeDto>>> Handle(FormTypeCascaderQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<FormTypeDto>> result = new ApiResult<List<FormTypeDto>>();
            result.code = ApiEnum.SearchSuccess;
            result.message = "成功";

            var list = from a in typeRep.GetAll()
                       where a.IndustryId == request.IndustryId
                       select new FormTypeDto
                       {
                           TypeNo = a.TypeNo,
                           TypeName = a.TypeName,
                       };
            result.data = list.ToList();

            return Task.FromResult(result);
        }
    }
}
